const app = getApp();
var openId = null
let jinbiShareLgImg = ''
let jinbiShareText = ''
let newsShareMinImg =''
Page({
  data: {
    yuanshengAd2:'adunit-d8272c6508073b96',
    jinbi: 0,
    xianjin: 0,
    jinbiShow: false,
    moreJinb: false,
    indexImg:''
  },
 
  
  onShow() {
    this.showMyjinbiNum()//动态显示积分数量
  },
  onLoad() {
    if(!app.globalData.isRegistered){
      app.selecteUser().then(res=>{
        app.globalData.isRegistered = res
      })
    }
    this.getController() //异步获取系统控制变量
    this.getMyJinBiNum() // 获取我的积分数量并保存到全局变量
  },
  // 获取我的积分数量并保存到全局变量
  getMyJinBiNum() {
    if (app.globalData.userInfo) {
      openId = app.globalData.userInfo._openid
      this.setData({
        indexImg:app.globalData.SysController.indexImg,
        jinbi: app.globalData.userInfo.jinbiNum,
        xianjin: app.globalData.userInfo.jinbiNum / 10,
      })
    }else{
      this.selectUser()
    }
  },
  //动态显示积分数量
  showMyjinbiNum(){
    wx.cloud.database().collection('user').where({
      _openid:openId
    }).get().then(res=>{
      this.setData({
        jinbi: res.data[0].jinbiNum,
        xianjin: res.data[0].jinbiNum / 10
      })
    })
  },
   // 查询积分数量并显示
   selectUser() {
    wx.cloud.callFunction({
      name: 'user',
      data: {
        tag: 'selecteUser'
      }
    }).then(res => {
      openId = res.result[0]._openid
      this.setData({
        jinbi: res.result[0].jinbiNum,
        xianjin: res.result[0].jinbiNum / 10
      })
    })
  },
  // 兑换
  duihuan() {
    wx.navigateTo({
      url: `/pages/my_tixian/my_tixian?jinbi=${this.data.jinbi}`,
    })
  },
  // 更多积分平台
  moreJinbi() {
    // console.log(app.globalData.gotoAppid.appid,'=============');
    wx.navigateToMiniProgram({
      appId: app.globalData.gotoAppid.appid,
      path: 'page/index/index?tag=moreJinbi',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: jinbiShareText,
      path: `/pages/news/news?shangjiOpenId=${app.globalData.userInfo._openid}`,
      imageUrl: jinbiShareLgImg,
    }
  },
 //异步获取系统控制变量
 getController() {
  if (app.globalData.SysController == null) {
    app.getController().then(res => {
      jinbiShareLgImg = res.jinbi_page_top_show.jinbiShareLgImg
      newsShareMinImg = res.jinbi_page_top_show.newsShareMinImg
      jinbiShareText = res.jinbi_page_top_show.jinbiShareText
      this.setData({
        moreJinb: res.controller_page.moreJinb,
        jinbiShow: res.controller_page.jinbiShow
      })
    })
  } else {
    jinbiShareLgImg = app.globalData.SysController.jinbi_page_top_show.jinbiShareLgImg
    jinbiShareMinImg = app.globalData.SysController.jinbi_page_top_show.jinbiShareMinImg
    jinbiShareText = app.globalData.SysController.jinbi_page_top_show.jinbiShareText
    this.setData({
      moreJinb: app.globalData.SysController.controller_page.moreJinb,
      jinbiShow: app.globalData.SysController.controller_page.jinbiShow,
      yuanshengAd2:app.globalData.SysController.ad.yuansheng2
    })
  }
},


})